package com.henanuniversity.controller;

import com.henanuniversity.po.DeliveryAddress;
import com.henanuniversity.service.DeliveryAddressService;
import com.henanuniversity.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class DeliveryAddressController {
        public Object listDeliveryAddressByUserId(HttpServletRequest request, HttpServletResponse response){
            String userId = request.getParameter("userId");
            DeliveryAddressService service=new DeliveryAddressServiceImpl();
            List<DeliveryAddress> addressList = service.listDeliveryAddressByUserId(userId);
            return addressList;
        }

    public int saveDeliveryAddress(HttpServletRequest request,HttpServletResponse response){
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.saveDeliveryAddress(deliveryAddress);
        return result;
    }


    public int removeDeliveryAddress(HttpServletRequest request, HttpServletResponse response){
        DeliveryAddressService service=new DeliveryAddressServiceImpl();
        int daId=Integer.parseInt(request.getParameter("daId"));
        int result = service.removeDeliveryAddress(daId);
        return result;
    }
    public DeliveryAddress getDeliveryAddressById(HttpServletRequest request, HttpServletResponse response){
        DeliveryAddressService service=new DeliveryAddressServiceImpl();
        int daId=Integer.parseInt(request.getParameter("daId"));
        DeliveryAddress address = service.getDeliveryAddressById(daId);
        return address;
    }
    public  int updateDeliveryAddress(HttpServletRequest request, HttpServletResponse response){
        DeliveryAddressService service=new DeliveryAddressServiceImpl();
        DeliveryAddress deliveryAddress=new DeliveryAddress();
        deliveryAddress.setDaId(Integer.valueOf(request.getParameter("daId")));
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        int result = service.updateDeliveryAddress(deliveryAddress);
        return result;
    }
}
